package com.inews.logic;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import weibo4android.http.AccessToken;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.inews.bean.Task;
import com.inews.ui.AccessTokenActivity;
import com.inews.ui.IWeiboActivity;
import com.inews.util.AuthUtil;
import com.inews.util.JavaScriptUtil;

public class MainService extends Service implements Runnable{

	
	/** 任务队列. */
	private static BlockingQueue<Task> taskQueue = new LinkedBlockingQueue<Task>();
	
	/** activities集合. */
	private static List<Activity> activities = new ArrayList<Activity>();
	
	/** 是否运行线程. */
	private boolean isRun = false;
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Task.WEIBO_LOGIN:
				//更新UI
				IWeiboActivity activity = (IWeiboActivity) getActivityByName("LoginActivity");
				activity.refresh(msg.obj);
				break;
			case Task.GET_ACCESS_TOKEN:
				//获取用户授权信息
				AccessTokenActivity tokenActivity = (AccessTokenActivity) getActivityByName("AccessTokenActivity");
				tokenActivity.refresh(msg.obj);
				break;
				
			
			default:
				break;
			}
		}

	};
	
	
	/**
	 * 获取activity.
	 *
	 * @param name the name
	 * @return the activity by name
	 */
	private Activity getActivityByName(String name) {
		if(!activities.isEmpty()){
			for (Activity activity : activities) {
				if(null != activity){
					if(activity.getClass().getName().indexOf(name) > 0){
						return activity;
					}
				}
			}
		}
		return null;
	};
	
	
	/**
	 * 添加Activity.
	 *
	 * @param activity the activity
	 */
	public static void addActivity(Activity activity){
		activities.add(activity);
	}
	
	
	/**
	 * 添加任务到任务队列中.
	 *
	 * @param task the task
	 */
	public static void newTask(Task task){
		try {
			taskQueue.put(task);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
	@Override
	public void onCreate() {
		isRun = true;
		Executors.newSingleThreadExecutor().submit(this);
	}
	
	
	@Override
	public void run() {
		while(isRun){
			try {
				//取出任务
				Task task = taskQueue.take();
				//执行任务
				doTask(task);
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	
	/**
	 * 执行任务.
	 *
	 * @param task the task
	 */
	private void doTask(Task task) {
		Message msg = handler.obtainMessage();
		msg.what = task.getTaskId();
		switch (task.getTaskId()) {
		case Task.WEIBO_LOGIN:
			System.out.println("doTask >>> user login");
			msg.obj = "登录成功";
			break;
			
		case Task.GET_ACCESS_TOKEN:
			//用户授权信息获取
			String pin = null;
			while(null == pin){
				pin = JavaScriptUtil.PIN;
			}
			AccessToken accessToken = null;
			while(accessToken == null){
				accessToken = AuthUtil.getAccessToken(pin);
			}
			msg.obj = accessToken;	
			break;

		default:
			break;
		}
		
		handler.sendMessage(msg);
	}


	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
